package com.utils;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.Paint.FontMetrics;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.TypedValue;

public class TextDrawable extends Drawable {

    private final String text;
    private final Paint paint;
    private FontMetrics fm;

    public TextDrawable(String text) {

        this.text = text;

        this.paint = new Paint();
        
//        paint.setColor(Color.WHITE);
        paint.setColor(Color.BLACK);
//        paint.setTextSize( 22f);
              
        paint.setAntiAlias(true);
        paint.setFakeBoldText(true);
//        paint.setShadowLayer(6f, 0, 0, Color.BLUE);
        paint.setStyle(Paint.Style.FILL);
        paint.setTextAlign(Paint.Align.LEFT);
  
        Rect bounds = new Rect();
        paint.getTextBounds(text.toCharArray(), 0, text.length(), bounds);      
//        bounds.set(bounds.left, bounds.top, bounds.right+5, bounds.bottom);
        setBounds(bounds);
        fm = paint.getFontMetrics();
    }

    public void setTextSize(float size){
    	paint.setTextSize( size);
    	Rect bounds = new Rect();
        paint.getTextBounds(text.toCharArray(), 0, text.length(), bounds);
//        bounds.set(bounds.left, bounds.top, bounds.right+5, bounds.bottom);
        setBounds(bounds);
        fm = paint.getFontMetrics();
    }
    
    public int getIntrinsicWidth() {
    	// TODO Auto-generated method stub
    	return getBounds().width();
    }
    public int getIntrinsicHeight() {
    	// TODO Auto-generated method stub
    	return getBounds().height();
    }
    public void draw(Canvas canvas) {
    	Rect b = getBounds();
//    	paint.setColor(Color.BLACK);
//    	canvas.drawRect( b, paint);
//    	paint.setColor(Color.WHITE);
    	float y = b.height() / 2 - (fm.ascent + fm.descent) / 2;
        canvas.drawText(text, 0, y, paint);
    }

    public void setAlpha(int alpha) {
        paint.setAlpha(alpha);
    }

    public void setColorFilter(ColorFilter cf) {
        paint.setColorFilter(cf);
    }

    public int getOpacity() {
        return PixelFormat.OPAQUE;
    }
}
